<html>

<head>
<meta charset="UTF-8">
<title>登录&注册</title>
<!--图标样式-->
<link rel='stylesheet' href='../static/css/all.min.css'>
<link rel="stylesheet" href="../static/css/style.css">
</head>

<body>
  <h2>医疗管理系统</h2>
	<div class="container" id="container">
		<div class="form-container sign-up-container">
			<form action="/regist" id="regest" onsubmit="return regest()">
				<h1>注册</h1>
				<input type="certId" name="certId" placeholder="身份证号(若注册管理员则不需要)" />
				<input type="username" name="username" placeholder="账号" />
				<input type="password" name="password" placeholder="密码" />
				<div>
				<button type="submit">注册</button><button type="reset">重置</button>
				</div>
				<span id="message2" style="color: red"></span>
			</form>
		</div>
		<div class="form-container sign-in-container">
			<form  id="login" onsubmit="return login()">
				<h1>登录</h1>
				<input type="username" name="username" placeholder="账号" value=""/>
				<input type="password"  name="password" placeholder="密码" value=""/>
				<div>
					<button type="submit">登录</button><button type="reset">重置</button>
				</div>
				<span id="message" style="color: red"></span>
			</form>
		</div>
		<div class="overlay-container">
			<div class="overlay">
				<div class="overlay-panel overlay-left">
					<h1>欢迎回来！</h1>
					<p>请您先登录的个人信息，进行操作。</p>
					<button class="ghost" id="signIn">登录</button>
				</div>
				<div class="overlay-panel overlay-right">
					<h1>你好朋友！</h1>
					<p>输入您的个人信息注册。</p>
					<button  class="ghost" id="signUp">注册</button>
				</div>
			</div>
		</div>
	</div>
  <script src="../static/js/layui.all.js"></script>
  <script src="../static/js/jquery-3.3.1.min.js"></script>
  <script type="text/javascript">
	  function regest(){
		  var d = {};
		  var t = $('#regest').serializeArray();//取出id为regest的表单数据并序列化为数组
		  $.each(t, function() {//通过一个遍历把t中的数据放入d中
			  d[this.name] = this.value;
		  });
		  var json=JSON.stringify(d);//通过转json字符串的函数将d中的数据转为json，通过ajax请求的方式传入到后端
		  $.ajax({
			  url:"/regest",//后端接口映射地址
			  type:'post',//发post请求
			  data:json,//传入的数据为json字符串格式
			  contentType: "application/json;charset=utf-8",//编码格式
			  success:function(data){//后端执行成功的返回值为data，data中会携带注册成功的信息
				  console.log(data.message);//在前端控制台中打印返回的信息
				  if(data.message=="注册成功"){
					  alert('注册成功！！！\n请返回登录页面进行登录或继续注册')//提示注册成功，请登陆或者继续注册
					  location.href="/hospital/login";//跳转到登陆页面
				  }
				  else {
					  $("#message2").text(data.message);//如果注册失败，就会出现在下面出现注册失败的错误信息

				  }
			  },
			  error:function (data) {//一般走到这一步都是数据类型，编码格式出错，跟我们的后端程序关系不大
				  layer.msg(data.message, {
					  time: 2000, //2s后自动关闭
				  });
			  }
		  });
		  return false
	  }

	  function login(){
		  var d = {};
		  var t = $('#login').serializeArray();
		  $.each(t, function() {
			  d[this.name] = this.value;
		  });
		  var json=JSON.stringify(d);

		  $.ajax({
			  url:"/login",
			  type:'post',
			  data:json,
			  contentType: "application/json;charset=utf-8",
			  success:function(data){
				  if(data.message.indexOf("登录成功")!=-1){
					if(data.message.indexOf("3")!=-1){
						location.href="/hospital/patient/index"
					}
					else  if(data.message.indexOf("2")!=-1){
						location.href="/doctor/seekMedicalAdvice"
					  }
					else if(data.message.indexOf("1")!=-1 ){
						location.href="/admin/doctorManage"
					  }

				  }
				  else {
					  $("#message").text(data.message);

				  }
			  },
			  error:function (data) {
				  layer.msg(data.message, {
					  time: 2000, //2s后自动关闭
				  });
			  }
		  });
		  return false
	  }
  </script>
	<script  src="../static/js/index.js"></script>
</body>

</html>
